package com.ruoyi.web.StateDemo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 所有的状态实现类
 */
@Component
public class AllState {

    /**
     * 当前状态只能支付和关闭订单
     */
    @Component
    class UnpayState extends AbstractOrderState{

        /**
         * 已支付状态
         */
        @Autowired
        private PaiedState paiedState;
        @Autowired
        private ClosedState closedState;


        /**
         *
         * @return
         */
        @Override
        public StateEnum getCurrentState() {
            return StateEnum.UNPAY;
        }
        /**
         *   支付
         */
        @Override
        public  void paidEvent(ContextState contextState){
            System.out.println("执行支付的业务逻辑");
            contextState.setOrderState(paiedState);
            System.out.println("修改后状态是："+contextState.getCurrentState());
        }

        /**
         * 取消订单
         */
        @Override
       public void cancelOrderEvent(ContextState contextState){
            System.out.printf("执行取消订单的操作");
            contextState.setOrderState(closedState);

       }

    }

    /**
     * 已支付状态
     * 申请退款 核销
     */
    @Component
    class PaiedState extends AbstractOrderState{

        @Autowired
        private RefundState refundState;
        @Autowired
        private FinshedState finshedState;

        @Override
        public StateEnum getCurrentState() {
            return StateEnum.PAIED;
        }

        /**
         * 申请退款
         * @param contextState
         */
        @Override
        public void applyRefundEvent(ContextState contextState) {
            System.out.println("执行申请退款的操作");
            contextState.setOrderState(refundState);
        }

        @Override
        public void verificationEvent(ContextState contextState) {
            System.out.println("执行核销操作");
            contextState.setOrderState(finshedState);
        }

    }
    /**
     * 订单关闭状态
     */
    @Component
    class ClosedState extends AbstractOrderState{

        @Override
        public StateEnum getCurrentState() {
            return StateEnum.CLOSED;
        }
    }

    /**
     * 订单完成状态
     */
    @Component
    class FinshedState extends AbstractOrderState{

        @Override
        public StateEnum getCurrentState() {
            return StateEnum.FINSH;
        }
    }
    /**
     * 申请退款
     */
    @Component
    class RefundState extends AbstractOrderState {
        @Autowired
        private ClosedState closedState;

        @Override
        public StateEnum getCurrentState() {
            return StateEnum.REFUND;
        }

        /**
         * 同意退款操作
         * @param contextState
         */
        @Override
        public void doRefundEvent(ContextState contextState){
            System.out.println("同意退款操作");
            contextState.setOrderState(closedState);

        }
    }


}
